package cn.lyh.FoodSystem.Service.ImpI;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.lyh.FoodSystem.Dao.EntryDao;
import cn.lyh.FoodSystem.Dao.StudyDao;
import cn.lyh.FoodSystem.Dto.PageInfo;
import cn.lyh.FoodSystem.Dto.Result;
import cn.lyh.FoodSystem.Entity.Entry;
import cn.lyh.FoodSystem.Entity.Study;
import cn.lyh.FoodSystem.Service.EntryService;
import cn.lyh.FoodSystem.Service.SpeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class EntryServiceImpI implements EntryService {

    @Autowired
    private  EntryDao entryDao;

    @Autowired
    private StudyDao studyDao;

    @Autowired
    private SpeedService speedService;

    /**
     * 添加报名信息
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Result addEntry(Entry entry) {
        Result result = new Result();
        try {
            if (entry.getCourseId() != null) {
                List<Entry> list = entryDao.findByCourseId(entry.getCourseId());
                boolean pass = list.stream().anyMatch(item -> "6".equals(item.getStatus()));
                if (pass || list.isEmpty()) {
                    entry.setStatus("1");
                    Entry res = entryDao.save(entry);
                    if (res.getId() != null) {
                        result.setCode(200);
                        result.setMsg("报名成功");
                    } else {
                        result.setCode(400);
                        result.setMsg("前端请求语法错误");
                    }
                } else {
                    result.setCode(400);
                    result.setMsg("您已报名，无需重复报名");
                }
            } else {
                result.setCode(400);
                result.setMsg("报名课程不能为空");
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 查询报名表信息(用户自己)
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Result queryEntry(Entry entry) {
        Result result = new Result();
        try {
         List<Entry> entryList= entryDao.findByUserId(entry.getUserId());
         if (entry.getStatus()!=null){
             List<Entry> filteredEntryList = entryList.stream()
                     .filter(entry1 -> entry.getStatus().equals(entry1.getStatus()))
                     .toList();
             result.setData(filteredEntryList);
         }else {
             result.setData(entryList);
         }
            result.setCode(200);
            result.setMsg("报名信息查询成功！");
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 查询报名表信息 后台
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Result queryEntryAll(PageInfo pageInfo) {
        Result result = new Result();
        try {
            Pageable pageable = PageRequest.of(pageInfo.getPage(), pageInfo.getLimit());
            Page<Entry> entryList = entryDao.findAll(pageable);
            result.setMsg("报名用户信息查询成功！");
            result.setCode(200);
            result.setData(entryList);
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

    /**
     * 更新报名表
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Result updateEntry(Entry entry) {
        Result result = new Result();
        try {
            Entry entry1= entryDao.getReferenceById(entry.getId());
            BeanUtil.copyProperties(entry,entry1,
                            CopyOptions.create().
                            setIgnoreNullValue(true).
                            setIgnoreError(true));
            entryDao.save(entry1);
            result.setCode(200);
            result.setMsg("更新成功！");
            //报名成功后分配学习视频
            if (entry.getStatus().equals("6")){
                Study study = new Study();
                study.setUserId(entry1.getUserId());
                study.setCourseId(entry1.getCourseId());
                study.setSpeed("0");
                study.setStatus("0");
                study.setStartTime(new Date());
                study.setVisitLong("0");
                study.setTotalLong("0");
                Study study1=studyDao.save(study);
                System.out.println(study1);
                if (study1.getId()!=null){
                    result=speedService.addSpeed(study1);
                    if (result.getCode()==200){
                        result.setMsg("报名成功！");
                    }
                }else {
                    result.setMsg("报名失败");
                    result.setCode(400);
                }
            }
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }



    /**
     * 删除报名表
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Result deleteEntry(Entry entry) {
        Result result = new Result();
        try {
            entryDao.deleteById(entry.getId());
            result.setCode(200);
            result.setMsg("删除成功！");
        } catch (Exception e) {
            result.setCode(500);
            result.setMsg("程序错误！");
            result.setData(e.getCause());
        }
        return result;
    }

}
